﻿using CommonLibrary;
using CommonLibrary.Models;
using CommonSite.Areas.API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLToolkit.Data.Linq;
using CommonSite.Common;
using CommonHelper;

namespace CommonSite.Areas.API.Controllers
{
    public class UserOrgPositionController : BaseAPIController
    {
        public ActionResult Query()
        {
            using (CommonDatabase db = new CommonDatabase())
            {
                var query = (from p in db.sys_UserOrgPosition
                             select new UserOrgPosition
                             {
                                 UserId = CryptHelper.Encrypt(p.UserId),
                                 UserName = p.sys_User.sys_UserInfo.UserName,
                                 OrgId = CryptHelper.Encrypt(p.OrgId),
                                 Org = p.sys_Org.Name,
                                 PositionId = CryptHelper.Encrypt(p.PositionId),
                                 Position = p.sys_Position.Name,
                                 ParentUserId = p.ParentUserId == null ? null : CryptHelper.Encrypt(p.ParentUserId),
                                 ParentUserName = p.ParentUser.sys_UserInfo.UserName
                             }).ToList();

                return Success(new
                {
                    Total = query.Count,
                    Rows = query
                });
            }
        }

        public ActionResult Create(UserOrgPosition userOrgPosition)
        {
            if (this.ModelState.IsValid)
            {
                try
                {
                    int userId, orgId, positionId;
                    int? parentUserId;
                    DecryptId(userOrgPosition,
                        out userId,
                        out orgId,
                        out positionId,
                        out parentUserId);

                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();

                        db.sys_UserOrgPosition.Insert(() => new sys_UserOrgPosition
                        {
                            UserId = userId,
                            OrgId = orgId,
                            PositionId = positionId,
                            ParentUserId = parentUserId
                        });

                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                }
            }

            return ArgumentFailed();
        }

        public ActionResult Delete(UserOrgPosition userOrgPosition)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    int userId, orgId, positionId;
                    int? parentUserId;
                    DecryptId(userOrgPosition,
                        out userId,
                        out orgId,
                        out positionId,
                        out parentUserId);

                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();
                        db.sys_UserOrgPosition.Where(
                            k => k.UserId == userId && k.OrgId == orgId &&
                                 k.PositionId == positionId && k.ParentUserId == parentUserId)
                          .Delete();

                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                    //return APIResult.Failed;
                }
            }

            return ArgumentFailed();
        }

        public void DecryptId(
            UserOrgPosition userOrgPosition,
            out int userId,
            out int orgId,
            out int positionId,
            out int? parentUserId)
        {
            userId = CryptHelper.DecryptToInt32(userOrgPosition.UserId);
            orgId = CryptHelper.DecryptToInt32(userOrgPosition.OrgId);
            positionId = CryptHelper.DecryptToInt32(userOrgPosition.PositionId);
            parentUserId = null;
            if (!userOrgPosition.ParentUserId.IsNullOrWhiteSpace() &&
                string.CompareOrdinal(userOrgPosition.ParentUserId, "null") != 0)
                parentUserId = CryptHelper.DecryptToInt32(userOrgPosition.ParentUserId);

            if (parentUserId == userId)
                parentUserId = null;
        }
    }
}
